package com.ruoyi.generator.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.generator.domain.GenForm;
import com.ruoyi.generator.domain.GenTable;
import com.ruoyi.generator.domain.vo.FromCrudBtnsVo;
import com.ruoyi.system.service.ISysMenuService;
import com.zgl.youshuda.core.utils.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.generator.mapper.FromCrudBtnsMapper;
import com.ruoyi.generator.domain.FromCrudBtns;
import com.ruoyi.generator.service.IFromCrudBtnsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 单设计__模块列区按钮配置Service业务层处理
 *
 * @author ZGL
 * @date 2025-01-17
 */
@Service
public class FromCrudBtnsServiceImpl implements IFromCrudBtnsService
{
    @Autowired
    private FromCrudBtnsMapper fromCrudBtnsMapper;

    @Autowired
    private ISysMenuService menuService;

    /**
     * 查询单设计__模块列区按钮配置
     *
     * @param id 单设计__模块列区按钮配置主键
     * @return 单设计__模块列区按钮配置
     */
    @Override
    public FromCrudBtns selectFromCrudBtnsById(Long id)
    {
        return fromCrudBtnsMapper.selectFromCrudBtnsById(id);
    }

    /**
     * 查询单设计__模块列区按钮配置列表
     *
     * @param fromCrudBtns 单设计__模块列区按钮配置
     * @return 单设计__模块列区按钮配置
     */
    @Override
    public List<FromCrudBtns> selectFromCrudBtnsList(FromCrudBtns fromCrudBtns)
    {
        return fromCrudBtnsMapper.selectFromCrudBtnsList(fromCrudBtns);
    }

    /**
     * 查询单设计__模块列区按钮配置列表
     */
    public List<FromCrudBtns> selectFromCrudBtnsByBuildKey(String buildKey){
        if(StringUtils.isEmptyIncludeNull(buildKey)){
            return null;
        }
        FromCrudBtns fromCrudBtns =new FromCrudBtns();
        fromCrudBtns.setBuildKey(buildKey);
        List<FromCrudBtns> fromCrudBtnList = fromCrudBtnsMapper.selectFromCrudBtnsList(fromCrudBtns);
        return fromCrudBtnList;
    }

    /**
     * 查询单设计__模块列区按钮配置列表与对应权限前缀
     */
    public Map<String,Object> selectFromCrudBtnsWithPermsPrefixByBuildKey(String buildKey,boolean isSimple){
        Map<String,Object> result=new HashMap<>();
        List<Map<String, String>> menuPermsInfo = menuService.menuPermsInfoByBuildKey(buildKey);
        if(menuPermsInfo!=null && menuPermsInfo.size()>0){
            String perms = menuPermsInfo.get(0).get("perms");
            String permsPrefix = perms.substring(0,perms.lastIndexOf(":"));
            result.put("permsPrefix",permsPrefix);
        }
        List<FromCrudBtns> fromCrudBtns = selectFromCrudBtnsByBuildKey(buildKey);
        if(isSimple){
            result.put("btnInfos",JSONArray.parseArray(JSONObject.toJSONString(fromCrudBtns), FromCrudBtnsVo.class));
        }else{
            result.put("btnInfos",fromCrudBtns);
        }
        return result;
    }

    /**
     * 获取所有设计key信息
     * @return
     */
    public Map<String, Map<String,Object>> getAllBuildKeyInfo(){

        List<Map<String, String>> allBuildKeyInfo = fromCrudBtnsMapper.getAllBuildKeyInfo();
        if(allBuildKeyInfo == null || allBuildKeyInfo.size()==0){
            return null;
        }
        /**
         * 遍历获取配置信息
         */
        Map<String,Map<String,Object>> result=new HashMap<>();

        for(Map<String, String> item : allBuildKeyInfo){
            result.put(item.get("build_key"),selectFromCrudBtnsWithPermsPrefixByBuildKey(item.get("build_key"),true));
        }

        return result;
    }

    /**
     * 新增单设计__模块列区按钮配置
     *
     * @param fromCrudBtns 单设计__模块列区按钮配置
     * @return 结果
     */
    @Override
    public int insertFromCrudBtns(FromCrudBtns fromCrudBtns) throws Exception
    {
        fromCrudBtns.setCreateTime(DateUtils.getNowDate());
        return fromCrudBtnsMapper.insertFromCrudBtns(fromCrudBtns);
    }

    /**
     * 新增单设计__模块列区按钮配置
     *
     * @param fromCrudBtns 单设计__模块列区按钮配置
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    public int refreshCrudBtns(String formBuildKey, List<FromCrudBtns> fromCrudBtns){
        /**
         * 删除之前按钮信息
         */
        fromCrudBtnsMapper.deleteFromCrudBtnsByBuildKey(formBuildKey);
        /**
         * 保存当前按钮配置信息
         */
        int count=0;
        if(fromCrudBtns==null || fromCrudBtns.size()<=0){
            return count;
        }
        for(int i=0;i<fromCrudBtns.size();i++){
            FromCrudBtns btn = fromCrudBtns.get(i);
            btn.setId(null);
            btn.setBuildKey(formBuildKey);
            btn.setSort(i);
            count+=fromCrudBtnsMapper.insertFromCrudBtns(btn);
        }
        return count;
    }

    public int initCrudBtns(GenForm genForm){
        String perms = genForm.getPerms();
        if(StringUtils.isNotEmptyIncludeNull(perms) && perms.indexOf(":")>-1){
            perms = perms.substring(0,perms.lastIndexOf(":"));
        }
        /**
         * 删除之前按钮信息
         */
        fromCrudBtnsMapper.deleteFromCrudBtnsByBuildKey(genForm.getFormBuildKey());

        if(!"VIEW".equals(genForm.getTableType())) {
            /**
             * 新增
             */
            fromCrudBtnsMapper.insertFromCrudBtns(new FromCrudBtns(genForm.getFormBuildKey(), "新增", "primary", "mini", "el-icon-plus", "Q", "// 新增操作\nthis.handleAdd()", perms+":add", 1, false, false, false,true));
            /**
             * 修改
             */
            fromCrudBtnsMapper.insertFromCrudBtns(new FromCrudBtns(genForm.getFormBuildKey(), "修改", "primary", "mini", "el-icon-edit", "A", "// 修改操作\nthis.handleUpdate()", perms+":edit", 2, false, false, false,true));
            /**
             * 删除
             */
            fromCrudBtnsMapper.insertFromCrudBtns(new FromCrudBtns(genForm.getFormBuildKey(), "删除", "primary", "mini", "el-icon-delete", "A", "// 删除操作\nthis.handleDelete()", perms+":remove", 3, true, false, false,true));
            /**
             * 查看详情
             */
            fromCrudBtnsMapper.insertFromCrudBtns(new FromCrudBtns(genForm.getFormBuildKey(), "详情", "primary", "mini", "el-icon-view", "A", "// 查看详情操作\nthis.handleDetail()", perms+":query", 4, true, false, false,false));
            /**
             * 导入
             */
            fromCrudBtnsMapper.insertFromCrudBtns(new FromCrudBtns(genForm.getFormBuildKey(),"导入","primary","mini","el-icon-upload2","Q","// 导入操作\nthis.handleImport()",perms+":import",5,true,false,false,true));
        }

        /**
         * 导出
         */
        fromCrudBtnsMapper.insertFromCrudBtns(new FromCrudBtns(genForm.getFormBuildKey(),"导出","primary","mini","el-icon-download","Q","// 导出操作\nthis.handleExport()",perms+":export",6,true,false,false,true));

        return 1;
    }

    /**
     * 修改单设计__模块列区按钮配置
     *
     * @param fromCrudBtns 单设计__模块列区按钮配置
     * @return 结果
     */
    @Override
    public int updateFromCrudBtns(FromCrudBtns fromCrudBtns)
    {
        fromCrudBtns.setUpdateTime(DateUtils.getNowDate());
        return fromCrudBtnsMapper.updateFromCrudBtns(fromCrudBtns);
    }

    /**
     * 批量删除单设计__模块列区按钮配置
     *
     * @param ids 需要删除的单设计__模块列区按钮配置主键
     * @return 结果
     */
    @Override
    public int deleteFromCrudBtnsByIds(Long[] ids)
    {
        return fromCrudBtnsMapper.deleteFromCrudBtnsByIds(ids);
    }

    /**
     * 删除单设计__模块列区按钮配置信息
     *
     * @param id 单设计__模块列区按钮配置主键
     * @return 结果
     */
    @Override
    public int deleteFromCrudBtnsById(Long id)
    {
        return fromCrudBtnsMapper.deleteFromCrudBtnsById(id);
    }
}
